package com.ztsoft.user.service.project.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztsoft.user.mapper.project.AgencyMapper;
import com.ztsoft.user.pojo.entity.project.Agency;
import com.ztsoft.user.pojo.vo.project.ProjectMeta;
import com.ztsoft.user.service.project.AgencyService;
import com.ztsoft.user.utils.Result;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AgencyServiceImpl extends ServiceImpl<AgencyMapper, Agency> implements AgencyService {

    @Override
    public Result<Object> updateAgency(Agency agency) {
        boolean isSuccess = this.updateById(agency);
        if (isSuccess) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> addAgency(Agency agency) {
        boolean isSuccess = this.save(agency);
        if (isSuccess) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> deleteAgency(String id) {
        boolean isSuccess = this.removeById(id);
        if (isSuccess) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> getAgencyMeta() {
        List<Agency> agencyList = list();
        ProjectMeta[] projectMetas = new ProjectMeta[agencyList.size()];

        List<ProjectMeta> projectMetaArrayList = new ArrayList<>();

        for (Agency agency : agencyList) {
            ProjectMeta projectMeta = new ProjectMeta();
            projectMeta.setValue(agency.getId());
            projectMeta.setLabel(agency.getAgency());
            projectMetaArrayList.add(projectMeta);
        }

        return Result.success(projectMetaArrayList.toArray(projectMetas));
    }

}